template<class T>
struct TreeNode
{
	TreeNode<T>* _left;
	TreeNode<T>* _right;
	T _data;

	TreeNode(const T& data)
		:_left(nullptr)
		,_right(nullptr)
		,_data(data)
	{}
};

template<class T>
class BSTree
{
public:
	typedef TreeNode<T> Node;
	
	BSTree() :_root(nullptr) {}
	BSTree(const BSTree<T>& bst) { _root = _CopyTree(bst._root); }

	BSTree<int>& operator=(BSTree<int> bst)
	{
		swap(_root, bst._root);
		return *this;
	}

	bool insert(const T& val)
	{
		if (_root == nullptr)
		{
			_root = new Node(val);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;

		while (cur)
		{
			if (cur->_data < val)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_data > val)
			{
				parent = cur;
				cur = cur->_left;
			}
			else break;
		}

		cur = new Node(val);
		if (parent->_data > val)
			parent->_left = cur;
		else
			parent->_right = cur;

		return true;
	}

	bool insertR(const T& val)
	{
		return _insertR(_root, val);
	}

	bool erase(const T& val)
	{
		if (!find(val))
			return false;

		//if (_root->_data == val && 
		// _root->_left == nullptr && 
		// _root->_right == nullptr)
		//{
		//	delete _root;
		//	_root = nullptr;
		//	return true;
		//}

		Node* cur = _root;
		Node* parent = nullptr;

		while (cur)
		{
			if (cur->_data > val)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_data < val)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				if (cur->_left == nullptr)
				{
					if (parent == nullptr)
					{
						_root = cur->_right;
					}
					else
					{
						if (parent->_left == cur)
							parent->_left = cur->_right;
						else
							parent->_right = cur->_right;
					}
				}
				else if (cur->_right == nullptr)
				{
					if (parent == nullptr)
					{
						_root = cur->_left;
					}
					else
					{
						if (parent->_left == cur)
							parent->_left = cur->_left;
						else
							parent->_right = cur->_left;
					}
				}
				else
				{
					Node* rightmin = cur->_right;
					Node* ParentRightmin = nullptr;
					while (rightmin->_left)
					{
						ParentRightmin = rightmin;
						rightmin = rightmin->_left;
					}
					
					swap(cur->_data, rightmin->_data);
					if (ParentRightmin == nullptr)
					{
						if (rightmin->_right == nullptr)
							cur->_right = nullptr;
						else
							cur->_right = rightmin->_right;
					}
					else
					{
						if (rightmin->_right == nullptr)
							ParentRightmin->_left = nullptr;
						else
							ParentRightmin->_left = rightmin->_right;
					}

					delete rightmin;
					return true;
				}

				delete cur;
				return true;
			}
		}

		return false;
	}

	bool eraseR(const T& val)
	{
		return _eraseR(_root, val);
	}

	bool find(const T& val)
	{
		Node* cur = _root;

		while (cur)
		{
			if (cur->_data < val)
				cur = cur->_right;
			else if (cur->_data > val)
				cur = cur->_left;
			else
				return true;
		}

		return false;
	}

	bool findR(const T& val)
	{
		return _findR(_root, val);
	}

	void InOrder()
	{
		_InOrder(_root);
	}

	~BSTree()
	{
		_Destroy(_root);
	}

private:
	Node* _CopyTree(Node* root)
	{
		if (root == nullptr)
			return nullptr;

		Node* copynode = new Node(root->_data);
		copynode->_left = _CopyTree(root->_left);
		copynode->_right = _CopyTree(root->_right);

		return copynode;
	}

	bool _insertR(Node*& root, const T& val)
	{
		if (root == nullptr)
		{
			root = new Node(val);
			return true;
		}

		if (root->_data < val)
			return _insertR(root->_right, val);
		else if (root->_data > val)
			return _insertR(root->_left, val);
		else
			return false;
	}

	bool _eraseR(Node*& root, const T& val)
	{
		if (root == nullptr)
			return false;

		if (root->_data < val)
			return _eraseR(root->_right, val);
		else if (root->_data > val)
			return _eraseR(root->_left, val);
		else
		{
			Node* del = root;
			if (root->_left == nullptr)
			{
				root = root->_right;
			}
			else if (root->_right == nullptr)
			{
				root = root->_left;
			}
			else
			{
				Node* leftmax = root->_left;
				while (leftmax->_right)
					leftmax = leftmax->_right;

				swap(root->_data, leftmax->_data);
				return _eraseR(root->_left, val);
			}

			delete del;
			return true;
		}
	}

	bool _findR(const Node* root, const T& val)
	{
		if (root == nullptr)
			return false;

		if (root->_data < val)
			return _findR(root->_right, val);
		else if (root->_data > val)
			return _findR(root->_left, val);
		else
			return true;
	}

	void _Destroy(Node*& root)
	{
		if (root == nullptr)
			return;

		_Destroy(root->_left);
		_Destroy(root->_right);
		delete root;
		root = nullptr;
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;

		_InOrder(root->_left);
		cout << root->_data << ' ';
		_InOrder(root->_right);
	}

	Node* _root;
};
